package org.stragma.apply.modules

import scala.collection.mutable._

import org.kiama.rewriting._
import org.kiama.rewriting.Rewriter.{all,attempt,fail,id,innermost,rulefs,Strategy,Term,termToStrategy,topdown,where}

// Import the required signatures, these are obtained from running sig2scala on the Stratego signatures
import org.stragma.apply.signatures.Apply._
import org.stragma.apply.signatures.Options._
import org.stragma.apply.util._

import org.stragma.util.Configuration._

/**
 *	Options is an approximate representation of the Option type
 *  from Stratego. In Stratego the option type can also store the
 * 	value of the option flag. In our case we do not store this flag
 *	as it would be redundant. The flag instead is the key of the optionsList Map
 */
case class Options(handlers : Strategy, msg : String)

object Options extends TermInputOutput {

	/**
	 * Interface: <get-config>OptionName  returns True if option specifed; fails otherwise.
	 */
	def get_option(optionName : TExp) : Strategy = {
		if (get_config(optionName).isDefined)
			id
		else
			fail
	}
	
	/**
	 * Stragma does not support the use of 
	 * custom options flags http://hydra.nixos.org/build/1246221/download/1/manual/chunk-chapter/library-options.html. So
	 * Instead we just use the Configuration implementation to approximate the same behaviour
	 */
	def set_options = set_config
	
	/**
	 * Specification of the defaults and mutually exclusive sets of options with
	 * priorities to choose in the event of conflict
	 */
	def do_defaults(a : Strategy) : Strategy = {
		where ( id <*
			// Test implies Simple
			attempt(get_option(Test()) <* set_config(Simple(),true)) 
			) <* a
	}
	
}